<!DOCTYPE HTML>
<!--generated with sswg-->
<html lang="en">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<head>
    <title> ursina engine documentation</title>
    <link rel="stylesheet" href="sswg.css">
    <link rel="stylesheet" href="style.css">
    <link rel="icon" type="image/x-icon" href="favicon.ico">
</head>
<body>
<div style="max-width:1200px; margin:auto;">
<div style="text-align:left;">
<br>
<a href="index.html"><img src="ursina_logo_wireframe.webp" style="width:50px; height:auto; margin-right:10px;"/></a> <a href=" installation.html" class="button">Download</a> <a href=" documentation.html" class="button">Documentation</a> <a href=" api_reference_v8_2_0/index.html" class="button">API Reference</a> <a href=" samples.html" class="button">Samples</a> <a href=" asset_store.html" class="button">Asset Store</a> <a href=" donate.html" class="button">Donate</a><br>
<h1 id="Tic Tac Toe">
Tic Tac Toe
</h1><a href="https://github.com/pokepetter/ursina/blob/master/samples/tic_tac_toe.py">https://github.com/pokepetter/ursina/blob/master/samples/tic_tac_toe.py</a><br>
<br>
<img src="icons/tic_tac_toe.jpg"></img> <br>
<code_block id="code_block_0"><button class="copy_code_button" onclick="copy_to_clipboard(code_block_0)">copy</button><purple>from</purple> ursina <purple>import</purple> *

<purple>if</purple> __name__ == <green>'__main__'</green>:
&nbsp;&nbsp;&nbsp;&nbsp;app = Ursina()

camera.orthographic = True
camera.fov = <yellow>4</yellow>
camera.position = (<yellow>1</yellow>, <yellow>1</yellow>)
Text.default_resolution *= <yellow>2</yellow>

player = <olive>Entity</olive>(name=<green>'o'</green>, <olive>color</olive>=color.azure)
cursor = Tooltip(player.name, <olive>color</olive>=player.color, <olive>origin</olive>=(<yellow>0</yellow>,<yellow>0</yellow>), <olive>scale</olive>=<yellow>4</yellow>, <olive>enabled</olive>=True)
cursor.background.color = color.clear
bg = <olive>Entity</olive>(<olive>parent</olive>=scene, <olive>model</olive>=<green>'quad'</green>, <olive>texture</olive>=<green>'shore'</green>, <olive>scale</olive>=(<yellow>1</yellow><yellow>6</yellow>,<yellow>8</yellow>), <olive>z</olive>=<yellow>1</yellow><yellow>0</yellow>, <olive>color</olive>=color.light_gray)
mouse.visible = False

<gray># create a matrix to store the buttons in. makes it easier to check <purple>for</purple> victory</gray>
board = [[None <purple>for</purple> x in <blue>range</blue>(<yellow>3</yellow>)] <purple>for</purple> y in <blue>range</blue>(<yellow>3</yellow>)]

<purple>for</purple> y in <blue>range</blue>(<yellow>3</yellow>):
&nbsp;&nbsp;&nbsp;&nbsp;<purple>for</purple> x in <blue>range</blue>(<yellow>3</yellow>):
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;b = Button(<olive>parent</olive>=scene, <olive>position</olive>=(x,y))
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;board[x][y] = b

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<purple>def</purple> on_click(b=b):
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;b.text = player.name
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;b.color = player.color
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;b.collision = False
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;check_for_victory()

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<purple>if</purple> player.name == <green>'o'</green>:
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;player.name = <green>'x'</green>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;player.color = color.orange
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<purple>else</purple>:
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;player.name = <green>'o'</green>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;player.color = color.azure

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cursor.text = player.name
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cursor.color = player.color

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;b.on_click = on_click


<purple>def</purple> check_for_victory():
&nbsp;&nbsp;&nbsp;&nbsp;name = player.name

&nbsp;&nbsp;&nbsp;&nbsp;won = (
&nbsp;&nbsp;&nbsp;&nbsp;(board[<yellow>0</yellow>][<yellow>0</yellow>].text == name and board[<yellow>1</yellow>][<yellow>0</yellow>].text == name and board[<yellow>2</yellow>][<yellow>0</yellow>].text == name) or <gray># across the bottom</gray>
&nbsp;&nbsp;&nbsp;&nbsp;(board[<yellow>0</yellow>][<yellow>1</yellow>].text == name and board[<yellow>1</yellow>][<yellow>1</yellow>].text == name and board[<yellow>2</yellow>][<yellow>1</yellow>].text == name) or <gray># across the middle</gray>
&nbsp;&nbsp;&nbsp;&nbsp;(board[<yellow>0</yellow>][<yellow>2</yellow>].text == name and board[<yellow>1</yellow>][<yellow>2</yellow>].text == name and board[<yellow>2</yellow>][<yellow>2</yellow>].text == name) or <gray># across the top</gray>
&nbsp;&nbsp;&nbsp;&nbsp;(board[<yellow>0</yellow>][<yellow>0</yellow>].text == name and board[<yellow>0</yellow>][<yellow>1</yellow>].text == name and board[<yellow>0</yellow>][<yellow>2</yellow>].text == name) or <gray># down the left side</gray>
&nbsp;&nbsp;&nbsp;&nbsp;(board[<yellow>1</yellow>][<yellow>0</yellow>].text == name and board[<yellow>1</yellow>][<yellow>1</yellow>].text == name and board[<yellow>1</yellow>][<yellow>2</yellow>].text == name) or <gray># down the middle</gray>
&nbsp;&nbsp;&nbsp;&nbsp;(board[<yellow>2</yellow>][<yellow>0</yellow>].text == name and board[<yellow>2</yellow>][<yellow>1</yellow>].text == name and board[<yellow>2</yellow>][<yellow>2</yellow>].text == name) or <gray># down the right side</gray>
&nbsp;&nbsp;&nbsp;&nbsp;(board[<yellow>0</yellow>][<yellow>0</yellow>].text == name and board[<yellow>1</yellow>][<yellow>1</yellow>].text == name and board[<yellow>2</yellow>][<yellow>2</yellow>].text == name) or <gray># diagonal /</gray>
&nbsp;&nbsp;&nbsp;&nbsp;(board[<yellow>0</yellow>][<yellow>2</yellow>].text == name and board[<yellow>1</yellow>][<yellow>1</yellow>].text == name and board[<yellow>2</yellow>][<yellow>0</yellow>].text == name))&nbsp;&nbsp; <gray># diagonal \</gray>

&nbsp;&nbsp;&nbsp;&nbsp;<purple>if</purple> won:
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<blue>print</blue>(<green>'winner is:'</green>, name)
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cursor.text = <green>''</green>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;mouse.visible = True
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Panel(<olive>z</olive>=<yellow>1</yellow>, <olive>scale</olive>=<yellow>1</yellow><yellow>0</yellow>, <olive>model</olive>=<green>'quad'</green>)
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;t = Text(f<green>'player\n{name}\nwon!'</green>, <olive>scale</olive>=<yellow>3</yellow>, <olive>origin</olive>=(<yellow>0</yellow>,<yellow>0</yellow>), background=True)
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;t.create_background(padding=(.<yellow>5</yellow>,.<yellow>2</yellow><yellow>5</yellow>), radius=Text.size/<yellow>2</yellow>)
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;t.background.color = player.color.tint(-.<yellow>2</yellow>)

<purple>if</purple> __name__ == <green>'__main__'</green>:
&nbsp;&nbsp;&nbsp;&nbsp;app.run()

</code_block>
<script>
function copy_to_clipboard(containerid) {
    var range = document.createRange()
    range.selectNode(containerid)
    window.getSelection().removeAllRanges()
    window.getSelection().addRange(range)
    document.execCommand("copy")
    window.getSelection().removeAllRanges()
}
</script>
<br>
<br>
</body>
</html>